/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.st.web.formula;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jeesite.modules.st.entity.StFormulaName;
import com.jeesite.modules.st.entity.StMaterialRecord;
import com.jeesite.modules.st.entity.StProductRecord;
import com.jeesite.modules.st.entity.formula.StFormulaDetail;
import com.jeesite.modules.st.pojo.AjaxJson;
import com.jeesite.modules.st.service.StMaterialRecordService;
import com.jeesite.modules.st.service.StProductRecordService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.st.entity.formula.StFormulaNameChild;
import com.jeesite.modules.st.service.formula.StFormulaNameService;

import java.util.Date;
import java.util.List;

/**
 * 生产配方Controller
 * @author HOHD
 * @version 2020-12-31
 */
@Controller
@RequestMapping(value = "${adminPath}/st/formula/stFormulaName")
public class StFormulaNameController extends BaseController {

	@Autowired
	private StFormulaNameService stFormulaNameService;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public StFormulaNameChild get(String id, boolean isNewRecord) {
		return stFormulaNameService.get(id, isNewRecord);
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("st:formula:stFormulaName:view")
	@RequestMapping(value = {"list",""})
	public String list(StFormulaNameChild stFormulaName, Model model) {
		model.addAttribute("stFormulaName", stFormulaName);
		return "modules/st/formula/stFormulaNameList";
	}
	@RequiresPermissions("st:formula:stFormulaName:view")
	@RequestMapping(value = {"id"})
    @ResponseBody
	public AjaxJson id(String productId, Model model) {
        StFormulaNameChild stFormulaNameChild = new StFormulaNameChild();
        stFormulaNameChild.setProductId(productId);
        AjaxJson<StFormulaNameChild> ajaxJson = new AjaxJson<>();
        ajaxJson.list=stFormulaNameService.findList(stFormulaNameChild);
        return ajaxJson;
	}
	@RequiresPermissions("st:formula:stFormulaName:view")
	@RequestMapping(value = {"detail"})
	@ResponseBody
	public AjaxJson detail(String id, Model model) {
		AjaxJson<StFormulaDetail> ajaxJson = new AjaxJson<>();
		ajaxJson.list=stFormulaNameService.get(id,false).getStFormulaDetailList();
		return ajaxJson;
	}
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("st:formula:stFormulaName:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<StFormulaNameChild> listData(StFormulaNameChild stFormulaName, HttpServletRequest request, HttpServletResponse response) {
		stFormulaName.setPage(new Page<>(request, response));
		Page<StFormulaNameChild> page = stFormulaNameService.findPage(stFormulaName);
		return page;
	}

	@Autowired
	StProductRecordService stProductRecordService;
	ObjectMapper objectMapper = new ObjectMapper();
	@Autowired
	StMaterialRecordService stMaterialRecordService;
	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("st:formula:stFormulaName:view")
	@RequestMapping(value = "form")
	public String form(StFormulaNameChild stFormulaName, Model model) throws JsonProcessingException {
		List<StProductRecord> list = stProductRecordService.findList(new StProductRecord());
		List<StMaterialRecord> materialList = stMaterialRecordService.findList(new StMaterialRecord());
		model.addAttribute("productList",list);
		model.addAttribute("materialList",objectMapper.writeValueAsString(materialList));
		model.addAttribute("stFormulaName", stFormulaName);
		return "modules/st/formula/stFormulaNameForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("st:formula:stFormulaName:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated StFormulaNameChild stFormulaName) {
		stFormulaNameService.save(stFormulaName);
		return renderResult(Global.TRUE, text("保存生产配方成功！"));
	}

	@RequiresPermissions("st:formula:stFormulaName:edit")
	@PostMapping(value = "copy")
	@ResponseBody
	public String copy(@Validated StFormulaNameChild stFormulaName) {
		stFormulaName.setId(null);
		List<StFormulaDetail> stFormulaDetailList = stFormulaName.getStFormulaDetailList();
		String code = stFormulaName.getCode();
		stFormulaName.setCode(code+"-2");
		stFormulaName.setUpdateDate(new Date());
		for (StFormulaDetail stFormulaDetail : stFormulaDetailList) {
			stFormulaDetail.setId(null);
			stFormulaDetail.setFormulaNameId(null);
		}
		stFormulaNameService.save(stFormulaName);
		return renderResult(Global.TRUE, text("保存生产配方成功！"));
	}


	/**
	 * 删除数据
	 */
	@RequiresPermissions("st:formula:stFormulaName:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(StFormulaNameChild stFormulaName) {
		stFormulaNameService.delete(stFormulaName);
		return renderResult(Global.TRUE, text("删除生产配方成功！"));
	}

}